package org.dromara.xhlj.crud.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.dromara.common.satoken.utils.LoginHelper;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.xhlj.domain.vo.XhljDiscussionVo;
import org.dromara.xhlj.domain.query.XhljDiscussionQuery;
import org.dromara.xhlj.domain.bo.XhljDiscussionBo;
import org.dromara.xhlj.crud.service.IXhljDiscussionCrudService;
import org.dromara.common.core.utils.tree.TreeListBuildUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * xhlj-crud-动态讨论
 *
 * @author yf
 * @date 2024-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xhlj/discussion")
public class XhljDiscussionCrudController extends BaseController {

    private final IXhljDiscussionCrudService xhljDiscussionService;

    /**
     * 分页查询动态讨论列表
     */
    @SaCheckPermission("xhlj:discussion:list")
    @GetMapping("/list")
    public TableDataInfo<XhljDiscussionVo> list(XhljDiscussionQuery query, PageQuery pageQuery) {
         return xhljDiscussionService.queryPageList(query, pageQuery);
    }

    /**
     * 查询动态讨论列表
     */
    @SaCheckPermission("xhlj:discussion:list")
    @GetMapping("/allList")
    public TableDataInfo<XhljDiscussionVo> allList(XhljDiscussionQuery query) {
        List<XhljDiscussionVo> list =  xhljDiscussionService.queryList(query);
        return TableDataInfo.build(list);
    }

    /**
     * 查询动态讨论树列表
     */
    @SaCheckPermission("xhlj:discussion:list")
    @GetMapping("/treeList")
    public TableDataInfo<XhljDiscussionVo> treeList(XhljDiscussionQuery query, PageQuery pageQuery) {
        TableDataInfo<XhljDiscussionVo> discussionVos =xhljDiscussionService.queryPageList(query, pageQuery);
        TreeListBuildUtil<XhljDiscussionVo> treeListUtil = new TreeListBuildUtil(XhljDiscussionVo.class);
        List<XhljDiscussionVo> treeList = treeListUtil.buildTree(discussionVos.getRows());
        return  TableDataInfo.build(treeList);
    }

    /**
     * 导出动态讨论列表
     */
    @SaCheckPermission("xhlj:discussion:export")
    @Log(title = "动态讨论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XhljDiscussionQuery query, HttpServletResponse response) {
        List<XhljDiscussionVo> list = xhljDiscussionService.queryList(query);
        ExcelUtil.exportExcel(list, "动态讨论", XhljDiscussionVo.class, response);
    }

    /**
     * 获取动态讨论详细信息
     *
     * @param discussionId 主键
     */
    @SaCheckPermission("xhlj:discussion:query")
    @GetMapping("/{discussionId}")
    public R<XhljDiscussionVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer discussionId) {
        return R.ok(xhljDiscussionService.queryById(discussionId));
    }

    /**
     * 新增动态讨论
     */
    @SaCheckPermission("xhlj:discussion:add")
    @Log(title = "动态讨论", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XhljDiscussionBo bo) {
        bo.setCreateBy(Math.toIntExact(LoginHelper.getUserId()));
        return toAjax(xhljDiscussionService.insertByBo(bo));
    }

    /**
     * 修改动态讨论
     */
    @SaCheckPermission("xhlj:discussion:edit")
    @Log(title = "动态讨论", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XhljDiscussionBo bo) {
        return toAjax(xhljDiscussionService.updateByBo(bo));
    }

    /**
     * 删除动态讨论
     *
     * @param discussionIds 主键串
     */
    @SaCheckPermission("xhlj:discussion:remove")
    @Log(title = "动态讨论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{discussionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] discussionIds) {
        return toAjax(xhljDiscussionService.deleteWithValidByIds(List.of(discussionIds), true));
    }
}
